import inspect
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from config import DB_URL, lib_path
from core.utils.module_utils import get_all_modules
from core.decorators.open_handler.index import OpenHandler

engine = create_engine(DB_URL)


class Environment:

    def initialize(self):
        self._init_db_()
        self._import_all_modules_()

    def _init_db_(self):
        self.db = sessionmaker(engine)()
        self.engine = engine

    def _import_all_modules_(self):
        """
        加载目录下所有的模型
        """
        _modules = []
        for _path in lib_path:
            _modules += get_all_modules(_path, "model")

        # 加载所有的类
        for _module in _modules:
            inspect.getmembers(_module, inspect.isclass)

    def get_all_handlers(self):
        # 注册路由
        open_handler = OpenHandler(["core"])
        return open_handler.get_handlers()
    
    def register_services(self):
        # 注册接口
        from core.decorators.open_api.index import openApiFactory
        openApiFactory.register_services(["core"]) # 主要是向services中添加注册的接口信息
environment = Environment()
